package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author XinxuanZhuo
 * @since 2024-02-05
 */
@Api(tags = "我的课表相关接口")
@Slf4j
@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
public class LearningLessonController {

    private final ILearningLessonService learningLessonService;


    /**
     * 分页查询我的课表
     */
    @ApiOperation("查询我的课表，排序字段 latest_learn_time:学习时间排序，create_time:购买时间排序")
    @GetMapping("/page")
    public PageDTO<LearningLessonVO> queryMyLessons(PageQuery query){
        return learningLessonService.queryMyLessons(query);
    }

    /**
     * 查询用户当前正在学习的课程
     */
    @ApiOperation("查询我正在学习的课程")
    @GetMapping("/now")
    public LearningLessonVO queryMyCurrentLesson(){
        return learningLessonService.queryMyCurrentLesson();
    }

    /**
     * 根据课程id，检查当前用户的课表中是否有该课程，课程状态是否有效。
     */
    @ApiOperation("检查当前用户的课表中是否有该课程，课程状态是否有效，是否可以学习该课程")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(@PathVariable Long courseId){
        return learningLessonService.isLessonValid(courseId);
    }

    /**
     * 根据课程id，查询当前用户的课表中是否有该课程，
     * 如果有该课程则需要返回课程的学习进度、课程有效期等信息。
     */
    @ApiOperation("根据课程id，查询当前用户的课表中是否有该课程")
    @GetMapping("/{courseId}")
    public LearningLessonVO queryLessonByCourseId(@PathVariable Long courseId){
        return learningLessonService.queryLessonByCourseId(courseId);
    }

    /**
     * 统计课程学习人数
     */
    @ApiOperation("统计课程学习人数")
    @GetMapping("/{courseId}/count")
    public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId){
        return learningLessonService.countLearningLessonByCourse(courseId);
    }

    /**
     * 创建学习计划
     */
    @ApiOperation("创建学习计划")
    @PostMapping("/plans")
    public void createLearningPlans(@Validated @RequestBody LearningPlanDTO planDTO){
        learningLessonService.createLearningPlan(planDTO.getCourseId(), planDTO.getFreq());
    }

    /**
     * 查寻学习计划进度
     */
    @ApiOperation("查寻学习计划进度")
    @GetMapping("/plans")
    public LearningPlanPageVO queryMyPlans(PageQuery query){
        return learningLessonService.queryMyPlans(query);
    }
}
